const puppeteer = require('puppeteer')
const config = require('./config')

async function start() {

    const browser = await puppeteer.launch(
        {
            headless: false,
            // executablePath: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
            executablePath: config.chromiumPath
        }
    );

    const page = await browser.newPage();

    page.on('console', msg => {
        for (let i = 0; i < msg.args().length; ++i)
            console.log(`${i}: ${msg.args()[i]}`);
    });

    await page.goto(`https://d.weibo.com/231650`, {
        // waitUntil: ['networkidle0']
    });

    let ret = []

    let count = 5
    for(let i = 0 ; i < count ;i++) {
        if(i !== 0) {
            await page.click('.page.next')
            await page.waitFor(2000)
        }

        await page.waitForSelector('.pt_ul')
        let items = await page.$$('.pt_ul li')
        for(let item of items) {
            ret.push(
                await item.evaluate(node => {
                    return {
                        '话题': node.querySelector('.S_txt1').innerText,
                        '阅读数': node.querySelector('.number').innerText,
                        '主持人': (node.querySelector('.tlink') || {}).innerText,
                        '标签': (node.querySelector('.W_btn_tag') || {}).innerText,
                    }
                })
            )
        }
    }

    console.table(ret)

    // let {skuName,skuPrice} = await page.evaluate(() => {
    //
    // })

    await browser.close();
}

start()